﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpLineFamilyInstance.h"
#include "DbObjectUtils.h"
#include "GmStructureDefaultElementParametersCustomizer.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElementParameters.h"
#include "IElementParentReporter.h"
#include "IElementPosition.h"
#include "IElementStatus.h"
#include "IExternalDataComponent.h"
#include "IFamily.h"
#include "IInstance.h"
#include "IInstanceType.h"
#include "IParameter.h"
#include "IPositionCurve2d.h"
#include "IPositionSingleAssociatedPlane.h"
#include "IRegenerator.h"
#include "LineFamilyInstanceInput.h"
#include "StructureInstanceLevelUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace gcmp
{
    class GbmpLineFamilyInstanceParametersCustomizer : public GmStructureDefaultElementParametersCustomizer
    {
        DBOBJECT_DATA_BEGIN(gcmp, GbmpLineFamilyInstanceParametersCustomizer,
            gcmp::GmStructureDefaultElementParametersCustomizer,
            C73E77E1 - 7D8B - 420B - A33D - 4F451773AB5E, gmstructure)
        DBOBJECT_DATA_END
    public:
        GbmpLineFamilyInstanceParametersCustomizer(IElement* pElement);

        virtual bool IsPresetParameter(const UniIdentity& paramDefUid) const override { return false; }
    };

    DBOBJECT_DATA_DEFINE(GbmpLineFamilyInstanceParametersCustomizer)
    {
        SetOwnerElement(nullptr);
    }

    GbmpLineFamilyInstanceParametersCustomizer::GbmpLineFamilyInstanceParametersCustomizer(
        IElement* pElement) : GmStructureDefaultElementParametersCustomizer(pElement)
    {
    }
}

DEFINE_GBMP_INSTANCE_METHODS(GbmpLineFamilyInstance);
DBOBJECT_DATA_DEFINE(GbmpLineFamilyInstance)
{
    m_pOwnerElement = nullptr;
}

GbmpLineFamilyInstance* GbmpLineFamilyInstance::Create(const LineFamilyInstanceInput* pInput, bool regenInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    IInstance* pInstance = nullptr;
    if (pInput->PositionType == ElementInputPositionType::Line)
    {
        pInstance = GbmpLineFamilyInstance::CreateStraightLineFamilyInstance(pInput, regenInstance);
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    GbmpLineFamilyInstance* pLineFamily = GbmpLineFamilyInstance::Get(pInstance);

    return pLineFamily;
}

IInstance* GbmpLineFamilyInstance::CreateStraightLineFamilyInstance(const LineFamilyInstanceInput* pInput, bool regenInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pInput->Document;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IInstanceType* pInstanceType = quick_cast<IInstanceType>(pDoc->GetElement(pInput->InstanceTypeId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstanceType, L"pInstanceType为空",L"GDMPLab",L"2024-03-30");

    IInstance* pInstance = pInstanceType->CreateInstance(L"通用线式族实例", pInput->CanBeShared, pInput->CreationOption,pInput->IsCreateGeometryRelationshipComponent);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    GbmpLineFamilyInstance* pLineFamily = GbmpLineFamilyInstance::RegisterExternalData(pInstance, pInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLineFamily, L"pLineFamily为空",L"GDMPLab",L"2024-03-30");

    ElementId associatedPlaneId = pInput->AssociatedPlaneId;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(associatedPlaneId.IsValid(), L"associatedPlaneId无效",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = StructureInstanceLevelUtils::GetLineFamilyInstanceStoreyLevelFromPositioningLevel(pDoc, associatedPlaneId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    bool bOk = StructureInstanceLevelUtils::CreateInstanceStoreyLevel(pInstance, pStoreyLevel);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"SetBeamStoreyLevel失败",L"GDMPLab",L"2024-03-30");

    ElementId baseId = pInput->BaseId.IsValid() ? pInput->BaseId : associatedPlaneId;
    bOk = IElementPosition::CreateCurve2dOnSingleAssociatedPlanePositionBehavior(pInstance, baseId, pInput->PositionPoints);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"PositionBehavior创建失败",L"GDMPLab",L"2024-03-30");

    IElementPosition* pPositionBehavior = pInstance->GetElementPosition();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionBehavior, L"pPositionBehavior为空",L"GDMPLab",L"2024-03-30");

    IPositionSingleAssociatedPlane* pPositionAssociatedPlane =
        quick_cast<IPositionSingleAssociatedPlane>(pPositionBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionAssociatedPlane, L"pPositionAssociatedPlane为空",L"GDMPLab",L"2024-03-30");
    pPositionAssociatedPlane->SetBasePlaneOffset(pInput->AssociatedPlaneOffset);

    if (pInstance->GetElementParameters())
    {
        OwnerPtr<IElementParametersCustomizer> opNewCustomizer = 
            NEW_AS_OWNER_PTR(GbmpLineFamilyInstanceParametersCustomizer, pInstance);
        GmStructureDefaultElementParametersCustomizer::SetElementParametersCustomizer(
            pInstance, TransferOwnership(opNewCustomizer));
    }

    const IPositionCurve2d* pPosCurve2d = dynamic_cast<const IPositionCurve2d*>(pPositionBehavior->GetPositionGeometry());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPosCurve2d, L"pPosCurve2d为空",L"GDMPLab",L"2024-03-30");
    bOk = pInstance->SetCoordinateSystemsByCurve(pPosCurve2d->GetBaseCurve());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"SetCoordinateSystemsByCurve失败",L"GDMPLab",L"2024-03-30");

    IElementStatus* pStatus = pInstance->GetStatus();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStatus, L"无法获取状态集",L"GDMPLab",L"2024-03-30");
    pStatus->SetIsVisible(pInput->Visible);

    // 生成形体
    ElementId elementId = pInstance->GetElementId();
    if (regenInstance && pDoc->GetRegenerator())
    {
        bOk = pDoc->GetRegenerator()->RegenerateElement(elementId);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"RegenerateElement失败",L"GDMPLab",L"2024-03-30");
    }

    return pInstance;
}

GbmpLineFamilyInstance* GbmpLineFamilyInstance::Get(IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        IExternalData* pData = pExternalDataComponent->FindExternalData(GbmpLineFamilyInstance::GetStaticClassSchema()->GetName());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");

        return quick_cast<GbmpLineFamilyInstance>(pData);
    }
    return nullptr;
}

const GbmpLineFamilyInstance* GbmpLineFamilyInstance::Get(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (const IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        const IExternalData* pData = pExternalDataComponent->FindExternalData(GbmpLineFamilyInstance::GetStaticClassSchema()->GetName());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");

        return quick_cast<const GbmpLineFamilyInstance>(pData);
    }
    return nullptr;
}

GbmpLineFamilyInstance* GbmpLineFamilyInstance::RegisterExternalData(IInstance* pInstance, const LineFamilyInstanceInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<GbmpLineFamilyInstance> opLineFamily = NEW_AS_OWNER_PTR(GbmpLineFamilyInstance);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opLineFamily, L"opBasicBeam为空",L"GDMPLab",L"2024-03-30");
    opLineFamily->SetOwnerElement(pInstance);

    GbmpLineFamilyInstance* pLineFamily = opLineFamily.get();
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        pExternalDataComponent->RegisterExternalData(GbmpLineFamilyInstance::GetStaticClassSchema()->GetName(), TransferOwnership(opLineFamily));
    }
    return pLineFamily;
}

bool GbmpLineFamilyInstance::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

IElement* GbmpLineFamilyInstance::GetOwnerElement()
{
    return m_pOwnerElement;;
}

const IElement* GbmpLineFamilyInstance::GetOwnerElement() const
{
    return m_pOwnerElement;
}

void GbmpLineFamilyInstance::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
}

void GbmpLineFamilyInstance::ReportParents(IElementParentReporter& reporter) const
{
}

void GbmpLineFamilyInstance::GetCalculators(ICalculatorCollection * calculators) const
{
}

void GbmpLineFamilyInstance::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds != nullptr, L"pParamDefUids为空",L"GDMPLab",L"2024-03-30");
}

OwnerPtr<IParameter> GbmpLineFamilyInstance::GetNativeParameter(int paramDefId) const
{
    return nullptr;
}

bool GbmpLineFamilyInstance::SetNativeParameter(const IParameter *param, std::wstring* errorMsg/* = nullptr*/)
{
    return false;
}

bool GbmpLineFamilyInstance::IsParameterModifiable(int paramDefId) const
{
    return true;
}

void GbmpLineFamilyInstance::Report(const ICopyContext & copyContext, ICopyStrategyReporter& reporter) const
{
    reporter.ReportToCopy(GetOwnerElementId());
}

NdbObject* GbmpLineFamilyInstance::GetTopOwnerObject() const
{
    return nullptr;
}

ElementId GbmpLineFamilyInstance::GetOwnerElementId() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetElementId() : ElementId::InvalidID;
}

IDocument* GbmpLineFamilyInstance::GetDocument() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetDocument() : nullptr;
}